﻿//function loadScript(url, callback) {
//    var head = document.getElementsByTagName('head')[0];
//    var script = document.createElement('script');
//    script.type = 'text/javascript';
//    script.src = url;
//    script.onreadystatechange = callback;
//    script.onload = callback;
//    head.appendChild(script);
//}

//loadScript('https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', function () {
    

// jQuery needed
// Custom added elements must have isCustom="true" attr

if (typeof (Igotta) == 'undefined')
    Type.registerNamespace('Igotta');

if (typeof (Igotta.StandartListFormsUtility) == 'undefined')
    Type.registerNamespace('Igotta.StandartListFormsUtility');


(function ($) {

    //
    // base field class

    Igotta.StandartListFormsUtility.FieldBase = function (internalName, scanObject) {
        this._internalName = internalName;

        if (scanObject) {
            this._initialize($(scanObject).contents().find('*'));
        }
        else {
            this._initialize($('*'));
        }
    };
    Igotta.StandartListFormsUtility.FieldBase.prototype = {
        _tr: null,
        _td: null,
        _fieldType: null,

        _initialize: function (scanElems) {
            var searchStr = 'FieldInternalName="' + this._internalName + '"';
            var self = this;
            scanElems.contents()
            .filter(function () { return (this.nodeType == 8 && this.nodeValue.indexOf(searchStr) >= 0); })
            .each(function (i, e) {
                self._td = $(e).parent();
                self._tr = $(e).parent().parent();
                self._fieldType = e.nodeValue.match(/FieldType="[a-z]+/ig)[0].substring(11); //FieldType="SPFieldLookup"
            });
        },

        getName: function () { return this._internalName; },
        getType: function () { return this._fieldType; },
        getRow: function () { return (this._tr); },
        getCol: function () { return (this._td); },
        getControl: function () {
            var spans = this._td.find('span[isCustom!="true"]');
            if (spans.length > 0) { return $(spans[0]); }
            return null;
        },

        // Mark every new tag with isCustom="true" attr
        getHTML: function () { return (this._td.html()); },
        setHTML: function (value) { this._td.html(value); },
        addHTML: function (value) { this._td.html(this._td.html() + value); },

        hide: function () { this._tr.hide(); },
        disable: function () {
            this._tr.find('input[isCustom!="true"],select[isCustom!="true"]').attr('readonly', 'readonly').addClass('readonly');
            this._tr.find('select[isCustom!="true"]').attr('disabled', 'disabled').addClass('readonly');

            var self = this;
            this._tr.find('input:checkbox[isCustom!="true"]').each(function () {
                $(this).hide();
                $(this).siblings('br').hide();
                if (this.checked) { self.addHTML('<span class="disabledcheckbox" isCustom="true">Yes</span>'); }
                else { self.addHTML('<span class="disabledcheckbox" isCustom="true">No</span>'); }
            });

            this._tr.find('a[isCustom!="true"]').hide();
            this._tr.find('img[isCustom!="true"]').hide();
        },

        hideDefaultControl: function () {
            this._tr.find('span[isCustom!="true"]').hide();
        }
    }
    Igotta.StandartListFormsUtility.FieldBase.registerClass('Igotta.StandartListFormsUtility.FieldBase');
        

    //
    // SPFieldText

    Igotta.StandartListFormsUtility.SPFieldText = function (internalName, scanObject) {
        Igotta.StandartListFormsUtility.SPFieldText.initializeBase(this, [internalName, scanObject]);
    };
    Igotta.StandartListFormsUtility.SPFieldText.prototype = {
        getValue : function () {
            var input = this._tr.find('input[isCustom!="true"]');
            if (input.length > 0) {
                return $(input[0]).val();
            }

            throw 'Get value error. Field internalname: ' + this._internalName;
        },

        setValue : function (value) {
            var input = this._td.find('input[isCustom!="true"]');
            if (input.length > 0) {
                $(input[0]).val(value);
                return;
            }

            throw 'Set value error. Field internalname: ' + this._internalName;
        }
    };
    Igotta.StandartListFormsUtility.SPFieldText.registerClass('Igotta.StandartListFormsUtility.SPFieldText', Igotta.StandartListFormsUtility.FieldBase);


    //
    // SPFieldLookup

    Igotta.StandartListFormsUtility.SPFieldLookup = function (internalName, scanObject) {
        Igotta.StandartListFormsUtility.SPFieldLookup.initializeBase(this, [internalName, scanObject]);
    };
    Igotta.StandartListFormsUtility.SPFieldLookup.prototype = {
        getValue: function () {
            // <= 20 items
            var select = this._tr.find('select[isCustom!="true"]');
            if (select.length > 0) {
                return $(select[0]).val();
            }
            // > 20 items
            var input = this._tr.find('input[isCustom!="true"]');
            if (input.length > 0) {
                return (document.getElementById(input[0].optHid)).value;
            }

            throw 'Get value error. Field internalname: ' + this._internalName;
        },

        setValue: function (value) {
            if (!(value >= 0)) return;

            // <= 20 items
            var select = this._tr.find('select[isCustom!="true"]');
            if (select.length > 0) {
                $(select[0]).val(value);
                return;
            }
            // > 20 items
            var input = this._tr.find('input[isCustom!="true"]');
            if (input.length > 0) {
                (document.getElementById(input[0].optHid)).value = value;
                return;
            }

            throw 'Set value error. Field internalname: ' + this._internalName;
        },

        setOnChangeHandler: function (handler) {
            // <= 20 items
            var select = this._tr.find('select[isCustom!="true"]');
            if (select.length > 0) {
                var self = this;
                $(select[0]).change(function () { handler(self); });
            }
            else { throw 'Method filterLookup not implement for lookups with > 20 items. Field internalname: ' + this._internalName; }
        },

        _allLookupOptions: null,
        filterLookup: function (lookupIdsToShow) {
            // <= 20 items
            var select = this._tr.find('select[isCustom!="true"]');
            if (select.length > 0) {
                this._allLookupOptions = [];

                var options = $(select[0]).children('option');
                for (var ii = 0; ii < options.length; ii++) {
                    this._allLookupOptions.push({ value: options[ii].value, text: options[ii].text });

                    var optinValue = parseInt(options[ii].value, 10);
                    if (optinValue != 0 && jQuery.inArray(optinValue, lookupIdsToShow) == -1) {
                        $(options[ii]).remove();
                    }
                }
            }

            // > 20 items
            var input = this._tr.find('input[isCustom!="true"]');
            if (input.length > 0) {

                var allChoices = input[0].choices.split('|');

                var choicesToShow = [];
                for (var ii in allChoices) {
                    var choiceId = parseInt(allChoices[ii], 10);
                    if (choiceId && jQuery.inArray(choiceId, lookupIdsToShow) != -1) {
                        choicesToShow.push(allChoices[ii - 1]);
                        choicesToShow.push(allChoices[ii]);
                    }
                }

                input[0].choices = choicesToShow.join('|');
            }
        },

        // clear filter
        filterLookupClear: function () {
            if (this._allLookupOptions == null) {
                return;
            }

            // <= 20 items
            var select = this._tr.find('select[isCustom!="true"]');
            if (select.length > 0) {

                // remove all options
                $(select[0]).children('option').remove().end();

                //_allLookupOptions
                for (var ii in this._allLookupOptions) {
                    $(select[0]).append($('<option>').text(this._allLookupOptions[ii].text).val(this._allLookupOptions[ii].value));
                }
            }
            else { throw 'Method filterLookupClear not implement for lookups with > 20 items. Field internalname: ' + this._internalName; }
        }
    };
    Igotta.StandartListFormsUtility.SPFieldLookup.registerClass('Igotta.StandartListFormsUtility.SPFieldLookup', Igotta.StandartListFormsUtility.FieldBase);


    //
    // SPFieldLookupMulti

    Igotta.StandartListFormsUtility.SPFieldLookupMulti = function (internalName, scanObject) {
        Igotta.StandartListFormsUtility.SPFieldLookupMulti.initializeBase(this, [internalName, scanObject]);
    };
    Igotta.StandartListFormsUtility.SPFieldLookupMulti.prototype = {
        filterLookupMulti: function (lookupIdsToShow) {
            var options = this._tr.find('select option');

            for (var ii = 0; ii < options.length; ii++) {
                if (jQuery.inArray(parseInt(options[ii].value, 10), lookupIdsToShow) == -1) {
                    $(options[ii]).remove();
                }
            }
        }
    };
    Igotta.StandartListFormsUtility.SPFieldLookupMulti.registerClass('Igotta.StandartListFormsUtility.SPFieldLookupMulti', Igotta.StandartListFormsUtility.FieldBase);


    //
    // SPFieldBoolean

    Igotta.StandartListFormsUtility.SPFieldBoolean = function (internalName, scanObject) {
        Igotta.StandartListFormsUtility.SPFieldBoolean.initializeBase(this, [internalName, scanObject]);
    };
    Igotta.StandartListFormsUtility.SPFieldBoolean.prototype = {
        getValue: function () {
            var input = this._tr.find('input[isCustom!="true"]');
            if (input.length > 0) {
                return $(input[0]).attr('checked');
            }

            throw 'Get value error. Field internalname: ' + this._internalName;
        },

        setValue: function (value) {
            var input = this._tr.find('input[isCustom!="true"]');
            if (input.length > 0) {
                $(input[0]).attr('checked', value);
                return;
            }

            throw 'Set value error. Field internalname: ' + this._internalName;
        }
    };
    Igotta.StandartListFormsUtility.SPFieldBoolean.registerClass('Igotta.StandartListFormsUtility.SPFieldBoolean', Igotta.StandartListFormsUtility.FieldBase);


    //
    // SPFieldDateTime

    Igotta.StandartListFormsUtility.SPFieldDateTime = function (internalName, scanObject) {
        Igotta.StandartListFormsUtility.SPFieldDateTime.initializeBase(this, [internalName, scanObject]);
    };
    Igotta.StandartListFormsUtility.SPFieldDateTime.prototype = {
        getValue: function () {
            var inputStr = this.getControl().find('.ms-dtinput input').val();
            if (!inputStr || inputStr.lenght == 0) {
                return null;
            }

            var date;

            var lcid = this._getSPFieldDateTimeLcid();
            switch (lcid) {
                case '1049':
                case '1031':
                    // dd.MM.yyyy
                    var dateParts = inputStr.split('.');
                    date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
                    break;
                case '1033':
                    // MM/dd/yyyy
                    var dateParts = inputStr.split('/');
                    date = new Date(dateParts[2], (dateParts[0] - 1), dateParts[1]);
                    break;
                case '1041':
                    // yyyy/MM/dd
                    var dateParts = inputStr.split('/');
                    date = new Date(dateParts[0], (dateParts[1] - 1), dateParts[2]);
                    break;
                default:
                    // dd/MM/yyyy
                    var dateParts = inputStr.split('/');
                    date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
                    break;
            }

            // get time
            var timeSelectors = this.getControl().find('.ms-dttimeinput select');
            if (timeSelectors.length > 1) {
                var hours = timeSelectors[0].selectedIndex;
                var minutes = timeSelectors[1].selectedIndex * 5;
                date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), hours, minutes);
            }

            return date;
        },

        setValue: function (value) {

            if (isNaN(Date.parse(value)) || value.format('d/M/yyyy HH:mm').indexOf('NaN') >= 0) { return; }

            var lcid = this._getSPFieldDateTimeLcid();
            var value_date = value.format(this._getDateFormatByLcid(lcid));
            var value_hour = value.getHours();
            var value_minute = Math.floor(value.getMinutes() / 5);

            var input_date = this.getControl().find('.ms-dtinput input');
            if (input_date.length > 0) {
                $(input_date[0]).val(value_date);
            }

            var input_time = this.getControl().find('.ms-dttimeinput select');
            if (input_time.length > 1) {

                var hours = $(input_time[0]).find('option');
                if (hours.length > value_hour) {
                    $(hours[value_hour]).attr('selected', 'selected');
                }

                var minutes = $(input_time[1]).find('option');
                if (minutes.length > value_minute) {
                    $(minutes[value_minute]).attr('selected', 'selected');
                }
            }
        },

        _getDateFormatByLcid: function (lcid) {
            var lcidsWithFormat_RUS = ';1031;1049;';
            var lcidsWithFormat_USA = ';1033;';
            var lcidsWithFormat_JPN = ';1041;';

            if (lcidsWithFormat_RUS.indexOf(';' + lcid + ';') >= 0)
            { return ('dd.MM.yyyy'); }

            if (lcidsWithFormat_USA.indexOf(';' + lcid + ';') >= 0)
            { return ('MM/dd/yyyy'); }

            if (lcidsWithFormat_JPN.indexOf(';' + lcid + ';') >= 0)
            { return ('yyyy/MM/dd'); }

            return ('dd/MM/yyyy');
        },

        _getSPFieldDateTimeLcid: function () {
            var lcid = '';
            var input_picker = this.getControl().find('.ms-dtinput a');
            if (input_picker.length > 0) {
                var pickerstring = $(input_picker[0]).attr('onclick').toString().toLowerCase();
                lcid = pickerstring.substr(pickerstring.indexOf('&lcid=') + 6).split('&')[0];
            }

            return lcid;
        }
    };
    Igotta.StandartListFormsUtility.SPFieldDateTime.registerClass('Igotta.StandartListFormsUtility.SPFieldDateTime', Igotta.StandartListFormsUtility.FieldBase);


    //
    // SPFieldBusinessData

    Igotta.StandartListFormsUtility.SPFieldBusinessData = function (internalName, scanObject) {
        Igotta.StandartListFormsUtility.SPFieldBusinessData.initializeBase(this, [internalName, scanObject]);
    };
    Igotta.StandartListFormsUtility.SPFieldBusinessData.prototype = {
        setValue: function (entityKey, displayText, displayBcsFieldName) {
            var controlId = $(this._td.children('span')[0]).children('span').attr('id');


            // make xml

            var xml =
                '<Entities Append="False" Error="" DoEncodeErrorMessage="False" MaxHeight="3">' +
                  '<Entity Key="' + entityKey + '" DisplayText="' + displayText + '" IsResolved="True" Description="">' +
                    '<ExtraData>' +
                      '<ArrayOfDictionaryEntry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
                        '<DictionaryEntry>' +
                          '<Key xsi:type="xsd:string">' + displayBcsFieldName + '</Key>' +
                          '<Value xsi:type="xsd:string">' + displayText + '</Value>' +
                        '</DictionaryEntry>' +
                      '</ArrayOfDictionaryEntry>' +
                    '</ExtraData>' +
                    '<MultipleMatches />' +
                  '</Entity>' +
                '</Entities>';


            EntityEditorCallback(xml, controlId);
            RunCustomScriptSetForPickerControl(controlId);
        }
    };
    Igotta.StandartListFormsUtility.SPFieldBusinessData.registerClass('Igotta.StandartListFormsUtility.SPFieldBusinessData', Igotta.StandartListFormsUtility.FieldBase);

})(jQuery);
    



//
// usage examples
//


////
//// SPFieldText

//// 'Title' - internal field name
//var field = new Igotta.StandartListFormsUtility.SPFieldText('Title');

//// set value
//field.setValue('TEST');

//// get value
//alert(field.getValue());

//// hide all row
//field.hide();

//// hide control
//field.hideDefaultControl();

//// disable
//field.disable();

//// paint control background
//var fieldTd = field.getCol();
//$(fieldTd).css('background-color', 'red');

//// paint control
//var fieldControl = field.getControl();
//$(fieldControl).css('background-color', 'green');

//// get internal name
//alert(field.getName());

//// get field type, will return 'SPFieldText'
//alert(field.getType());


////
//// SPFieldLookup

//// 'Lookup' - internal field name
//field = new Igotta.StandartListFormsUtility.SPFieldLookup('incType');

//// set value, 1 - is ID of lookup list element
//field.setValue(1);

//// get value, return ID of selected list element
//console.log(field.getValue());

//// make available only elements with specified ids
//field.filterLookup([1]);

//// clear filter - show all options
//field.filterLookupClear();

//// set OnChangeHandler
//field.setOnChangeHandler(function (senderField) { console.log(senderField.getValue()); });


//// all examples for SPFieldText is applicable


////
//// SPFieldLookupMulti (currently support only one method - filterLookupMulti)

//// 'LookupMulti' - internal filed name
//var field = new Igotta.StandartListFormsUtility.SPFieldLookupMulti('LookupMulti');

//// make available only elements with specified ids 
//field.filterLookupMulti([1, 2, 6]);


////
//// SPFieldDateTime (applicable for all cultures)

//// 'Date' - internal field name
//var field = new Igotta.StandartListFormsUtility.SPFieldDateTime('Date');

//// set value
//field.setValue(new Date());

//// get value, will return Date object
//alert(field.getValue());

//// all examples for SPFieldText is applicable


////
//// SPFieldBoolean

//// 'Bool' - internal field name
//var field = new Igotta.StandartListFormsUtility.SPFieldBoolean('Bool');

//// set value
//field.setValue(true);

//// get value
//alert(field.getValue());

//// all examples for SPFieldText is applicable


////
////SPFieldBusinessData (test mode)

//// 'Bcs' - internal field name
//var field = new Igotta.StandartListFormsUtility.SPFieldBusinessData('Bcs');
//field.setValue('entityKey', 'displayText', 'displayBcsFieldName');


//});